# -*- coding: utf-8 -*-
"""
Created on Sun Oct 11 10:09:35 2020
用于双量子点量子模型
单量子比特的归零操作
多个初始态对多个目标态的归零保真度

@author: Waikikilick
"""

import numpy as np
from scipy.linalg import expm
from time import *
import multiprocessing as mp
np.random.seed(1)
sx = np.mat([[0, 1], [1, 0]], dtype=complex)
sy = np.mat([[0, -1j], [1j, 0]], dtype=complex)
sz = np.mat([[1, 0], [0, -1]], dtype=complex)

# a0,a1,a2,a3,a4,a5,a6,a7,a8,a9 = 0,0,0,0,0,0,0,0,0,0 #统计各动作被选用的频率

action_space = np.mat([[1,0,0], #可以选择的动作范围，各列的每项分别代表着 sigma x, y, z 前面的系数。
                       [2,0,0], #每次执行的动作都是单独的绕 x, y, z 轴一定角度的旋转
                       [0,1,0], # x, y 方向的值可以取负，但 z 方向的只能取正值
                       [0,2,0],
                       [0,0,1],
                       [0,0,2],
                       [-1,0,0],
                       [-2,0,0],
                       [0,-1,0],
                       [0,-2,0]])

theta_num = 6 #除了 0 和 Pi 两个点之外，点的数量
varphi_num = 21#varphi 角度一圈上的点数

theta = np.linspace(0,np.pi,theta_num+2,endpoint=True) 
varphi = np.linspace(0,np.pi*2,varphi_num,endpoint=False) 

def psi_set():
    psi_set = []
    for ii in range(1,theta_num+1):
        for jj in range(varphi_num):
            psi_set.append(np.mat([[np.cos(theta[ii]/2)],[np.sin(theta[ii]/2)*(np.cos(varphi[jj])+np.sin(varphi[jj])*(0+1j))]]))
    psi_set.append(np.mat([[1], [0]], dtype=complex))
    psi_set.append(np.mat([[0], [1]], dtype=complex))
    return psi_set

#动作直接选最优的
def step(psi,target_psi,F):
    fid_list = []
    psi_list = []
    action_list = list(range(len(action_space)))
    for action in action_list:
        H = float(action_space[action,0])*sx/2 + float(action_space[action,1])*sy/2 - float(action_space[action,2])*sz/2
        U = expm(-1j * H * dt) 
        psi_ = U * psi
        fid = (np.abs(psi_.H * target_psi) ** 2).item(0).real 
        psi_list.append(psi_)
        fid_list.append(fid)
        best_action = fid_list.index(max(fid_list))
        best_fid = max(fid_list)
    psi_ = psi_list[best_action]
    # print(best_action)
    return best_action, best_fid, psi_

#动作选最优的，或者最差的
def step1(psi,target_psi,F):
    fid_list = []
    psi_list = []
    action_list = list(range(len(action_space)))
    for action in action_list:
        
        H = float(action_space[action,0])*sx/2 + float(action_space[action,1])*sy/2 - float(action_space[action,2])*sz/2
        U = expm(-1j * H * dt) 
        psi_ = U * psi
        fid = (np.abs(psi_.H * target_psi) ** 2).item(0).real 
        
        psi_list.append(psi_)
        fid_list.append(fid)
    
    if F < max(fid_list):
        best_action = fid_list.index(max(fid_list))
        best_fid = max(fid_list)
    else:
        # del action_list[fid_list.index(max(fid_list))]
        # del psi_list[fid_list.index(max(fid_list))]
        # del fid_list[fid_list.index(max(fid_list))]
        
        # best_action = fid_list.index(max(fid_list))
        # best_fid = max(fid_list)
        
        best_action = fid_list.index(min(fid_list))
        best_fid = min(fid_list)
    psi_ = psi_list[best_action]
    # print(best_action)
    return best_action, best_fid, psi_

#动作选最优的，或者次优的
def step2(psi,target_psi,F):
    fid_list = []
    psi_list = []
    action_list = list(range(len(action_space)))
    for action in action_list:
        
        H = float(action_space[action,0])*sx/2 + float(action_space[action,1])*sy/2 - float(action_space[action,2])*sz/2
        U = expm(-1j * H * dt) 
        psi_ = U * psi
        fid = (np.abs(psi_.H * target_psi) ** 2).item(0).real 
        
        psi_list.append(psi_)
        fid_list.append(fid)
        
    if F < max(fid_list):
        best_action = fid_list.index(max(fid_list))
        best_fid = max(fid_list)
    else:
        del action_list[fid_list.index(max(fid_list))]
        del psi_list[fid_list.index(max(fid_list))]
        del fid_list[fid_list.index(max(fid_list))]
        
        best_action = fid_list.index(max(fid_list))
        best_fid = max(fid_list)
        
        # best_action = fid_list.index(min(fid_list))
        # best_fid = min(fid_list)
    psi_ = psi_list[best_action]
    # print(best_action)
    return best_action, best_fid, psi_
 
def job(target_psi):
    fid_list = []
    for psi1 in init_set:
        
        psi = psi1
        F = (np.abs(psi1.H * target_psi) ** 2).item(0).real 
        fid_max = 0
        fid_max1 = 0
        fid_max2 = 0
        fid_max0 = 0
        step_n = 0
        while True:
            action, F, psi_ = step1(psi,target_psi,F)
            fid_max1 = max(F,fid_max1)
            psi = psi_
            step_n += 1
            if fid_max1>0.999 or step_n>step_max:
                break
            
        step_n = 0
        F = (np.abs(psi1.H * target_psi) ** 2).item(0).real 
        psi = psi1
        while True:
            action, F, psi_ = step2(psi,target_psi,F)
            fid_max2 = max(F,fid_max2)
            psi = psi_
            step_n += 1
            if fid_max2>0.999 or step_n>step_max:
                break 
            
        fid_max = max(fid_max1,fid_max2)
        if fid_max < 0.99:
            step_n = 0
            F = (np.abs(psi1.H * target_psi) ** 2).item(0).real 
            psi = psi1
            while True:
                action, F, psi_ = step(psi,target_psi,F)
                fid_max0 = max(F,fid_max0)
                psi = psi_
                step_n += 1
                if fid_max0>0.999 or step_n>step_max:
                    break 
            fid_max = max(fid_max,fid_max0)  
        fid_list.append(fid_max)
    return  np.mean(fid_list)

def multicore():
    pool = mp.Pool()
    F_list = pool.map(job, target_set)
    return F_list
    

if __name__ == '__main__':
    target_set = psi_set()
    init_set = psi_set()
    # print(target_set)
    time1 = time()
    for kT in [1,2,3,4]:
        for kdt in [2,3,5,10,20,30,40]:
            F_list = []
            T = kT*np.pi
            dt = np.pi/kdt
            step_max = T/dt
            F_list = multicore()
            print("kT = ",kT,"kdt = ",kdt,"mean_fid = ",np.mean(F_list))
    time2 = time()
    print('time_cost is: ',time2-time1)
    
# kT =  1 kdt =  2 mean_fid =  0.9635456774393174
# kT =  1 kdt =  3 mean_fid =  0.9828572501218583
# kT =  1 kdt =  5 mean_fid =  0.9936352220910674
# kT =  1 kdt =  10 mean_fid =  0.9980008013073223
# kT =  1 kdt =  20 mean_fid =  0.999371576137948
# kT =  1 kdt =  30 mean_fid =  0.9995693008655068
# kT =  1 kdt =  40 mean_fid =  0.9995996120014894
# kT =  2 kdt =  2 mean_fid =  0.966716839974876
# kT =  2 kdt =  3 mean_fid =  0.9854810470232036
# kT =  2 kdt =  5 mean_fid =  0.9943481723879608
# kT =  2 kdt =  10 mean_fid =  0.9980753917120109
# kT =  2 kdt =  20 mean_fid =  0.9994235761179195
# kT =  2 kdt =  30 mean_fid =  0.999602071122402
# kT =  2 kdt =  40 mean_fid =  0.9996245050595272
# kT =  3 kdt =  2 mean_fid =  0.9667769702575821
# kT =  3 kdt =  3 mean_fid =  0.9859903943286914
# kT =  3 kdt =  5 mean_fid =  0.9944236974938392
# kT =  3 kdt =  10 mean_fid =  0.9980925639520126
# kT =  3 kdt =  20 mean_fid =  0.9994305965721791
# kT =  3 kdt =  30 mean_fid =  0.9996031282108856
# kT =  3 kdt =  40 mean_fid =  0.9996246285746572
# kT =  4 kdt =  2 mean_fid =  0.9667769702575821
# kT =  4 kdt =  3 mean_fid =  0.9861592277827697
# kT =  4 kdt =  5 mean_fid =  0.9944598731743686
# kT =  4 kdt =  10 mean_fid =  0.9981044824567785
# kT =  4 kdt =  20 mean_fid =  0.9994336647940384
# kT =  4 kdt =  30 mean_fid =  0.9996032612118781
# kT =  4 kdt =  40 mean_fid =  0.9996246285746572
# time_cost is:  1037.3249399662018
